package com.ruoyi.peach.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.peach.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.peach.service.IArticlesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author ld
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/peach/articles")
public class ArticlesController extends BaseController
{
    @Autowired
    private IArticlesService articlesService;

    /**
     * 查询文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:list')")
    @GetMapping("/list")
    public TableDataInfo list(Articles articles)
    {
        startPage();
        List<Articles> list = articlesService.selectArticlesList(articles);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('peach:articles:list')")
    @GetMapping("/list/app")
    public TableDataInfo listApp(Articles articles)
    {
        startPage();
        List<Articles> list = articlesService.selectArticlesListApp(articles);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Articles articles)
    {
        List<Articles> list = articlesService.selectArticlesList(articles);
        ExcelUtil<Articles> util = new ExcelUtil<Articles>(Articles.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") String articleId)
    {
        return success(articlesService.selectArticlesByArticleId(articleId));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Articles articles)
    {
        return toAjax(articlesService.insertArticles(articles));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Articles articles)
    {
        return toAjax(articlesService.updateArticles(articles));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('peach:articles:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable String[] articleIds)
    {
        return toAjax(articlesService.deleteArticlesByArticleIds(articleIds));
    }
    /**
     * 发布文章一级评论
     *
     *评论数也加1
     */
    @PostMapping("/comment1")
    public AjaxResult commentOne(@RequestBody Comment comment){
        return  toAjax(articlesService.commentOne(comment)) ;
    }
    @PostMapping("/comment2")
    public AjaxResult commentOne(@RequestBody Comment2 comment){
        return  toAjax(articlesService.commentTow(comment)) ;
    }
    /**
     * 获取所有一级评论
     *
     */
    @GetMapping("/comment1")
    public  TableDataInfo listComment(String articleId){

        List<Comment4> list = articlesService.selectComment1List(articleId);
        return getDataTable(list);

    }
    @GetMapping("/comment2")
    public  TableDataInfo listComment2(String commentId){

        List<Comment3> list = articlesService.selectComment2List(commentId);
        return getDataTable(list);

    }
}
